package com.pi.controller;

import com.pi.po.ReportPO;
import com.pi.util.MySql;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

public class PublishGetData extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String,String[]> map=req.getParameterMap();
        String userId=map.get("user_id")[0];
        String userReportId=map.get("user_report_id")[0];
        int reportColumnNumber=Integer.parseInt(map.get("report_column_number")[0]);
        System.out.println(userId+","+userReportId+","+reportColumnNumber);

        String sqlHead="select * from report where user_id=? and user_report_id=?";
        String sqlData="select * from report_data where user_id=? and user_report_id=?";
        Connection connection= MySql.getConnection();
        String str="";
        int count=0;
        try {
            //获取头部
            PreparedStatement statement=connection.prepareStatement(sqlHead);
            statement.setString(1,userId);
            statement.setString(2,userReportId);
            ResultSet resultSet=statement.executeQuery();
            while(resultSet.next()){
                count++;
                for(int i=1;i<=reportColumnNumber;i++){
                    str=str+resultSet.getString("t"+i)+";";
                }
            }
            System.out.println(str);
            //获取数据
            statement=connection.prepareStatement(sqlData);
            statement.setString(1,userId);
            statement.setString(2,userReportId);
            resultSet=statement.executeQuery();
            while(resultSet.next()){
                count++;
                for(int i=1;i<=reportColumnNumber;i++){
                    str=str+resultSet.getString("t"+i)+";";
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().println(count+";"+reportColumnNumber+";"+str);
    }
}
